package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.activity.WebViewActivity;
import com.youmingxing.adpter.selfInfo.MyTicketsAdapter;
import com.youmingxing.bean.Web;
import com.youmingxing.bean.selfinfo.MyTicket;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.utils.TimeUtils;
import com.youmingxing.view.DialogManager;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author evilyin(ChenZhixi)
 * @since 2015-9-1
 * 我的加油票
 */

public class MyTicketsActivity extends BaseActivity implements RefreshListView.OnLoadMoreListener {

    private List<MyTicket> myTicketList;
    private View headerView;
    private MyTicketsAdapter adapter;
    private RefreshListView listView;
    private TextView methodTv;
    private TextView totalTv;
    private TextView timeTv;
    private int mark = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_list_view);
        setTopbarName(R.string.mytickets_title);
        myTicketList = new ArrayList<>();
        listView = (RefreshListView) findViewById(R.id.list_view);
        DialogManager.showDialog(this);
        SelfInfoApi.getMyTicket(mark, ticketHandler);
    }

    @Override
    public void onErrorClick() {
        SelfInfoApi.getMyTicket(mark, ticketHandler);
    }

    private void initView() {
        listView.setOnLoadMoreListener(this);
        adapter = new MyTicketsAdapter(this);
        listView.setAdapter(adapter);
        headerView = getLayoutInflater().inflate(R.layout.activity_my_tickets_top, null);
        totalTv = (TextView) headerView.findViewById(R.id.my_tickets_num);
        timeTv = (TextView) headerView.findViewById(R.id.tv_time);
        methodTv = (TextView) headerView.findViewById(R.id.my_tickets_useful_method);
        methodTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyTicketsActivity.this, WebViewActivity.class);
                intent.putExtra("web_page", Web.tickets);
                startActivity(intent);
            }
        });
        listView.addHeaderView(headerView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            }
        });
    }

    @Override
    public void onLoadMore() {
        SelfInfoApi.getMyTicket(mark, ticketHandler);
    }

    BaseJsonHandler ticketHandler = new BaseJsonHandler(this) {
        public void onSuccessWithData(JSONObject response) {
            listView.endLoadMore();
            if (headerView == null) {
                initView();
            }
            showErrorPage(false);
            JSONArray data = response.optJSONObject("data").optJSONArray("ballots");
            if (mark == 1 && data.length() == 0) {
                listView.noData("您当前还没有加油票哦");
            }
            mark++;
            int total = response.optJSONObject("data").optInt("valid_count");
            if (data.length() < 20) {
                listView.noMoreData();
            }
            totalTv.setText(total + "");
            ((TextView) findViewById(R.id.my_tickets_usefulnum)).setText("我的加油票: " + total + "张有效");
            int genTime = response.optJSONObject("data").optInt("gen_duetime");
            new CountDownTimer(genTime * 1000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    timeTv.setText(TimeUtils.parseSecondToString(millisUntilFinished / 1000));
                }

                @Override
                public void onFinish() {
                    //重新获取
                    mark = 1;
                    SelfInfoApi.getMyTicket(mark, ticketHandler);
                }
            }.start();
            for (int i = 0; i < data.length(); i++) {
                MyTicket bean = new MyTicket(data.optJSONObject(i));
                myTicketList.add(bean);
            }
            adapter.setData(myTicketList);
        }

        @Override
        public void onFail(int statusCode, String statusText) {
            super.onFail(statusCode, statusText);
            listView.endLoadMore();
            if (myTicketList == null || myTicketList.size() <= 0) {
                showErrorPage(true);
            }
        }
    };
}
